package exam;

import java.util.List;
import java.util.Scanner;

/**
 * 用户DAO测试类 - 动态输入版
 */
public class DaoTest {
    private static Scanner scanner = new Scanner(System.in);
    private static UserDao userDao = new UserDaoImpl();

    public static void main(String[] args) {
        // 初始化数据库表
        userDao.createTable();

        while (true) {
            System.out.println("\n========== 用户管理系统 ==========");
            System.out.println("1. 添加用户");
            System.out.println("2. 用户登录");
            System.out.println("3. 修改用户");
            System.out.println("4. 删除用户");
            System.out.println("5. 查询所有用户");
            System.out.println("6. 分页查询用户");
            System.out.println("7. 按编号查询用户");
            System.out.println("0. 退出系统");
            System.out.print("请选择操作: ");

            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    addUser();
                    break;
                case 2:
                    userLogin();
                    break;
                case 3:
                    updateUser();
                    break;
                case 4:
                    deleteUsers();
                    break;
                case 5:
                    listAllUsers();
                    break;
                case 6:
                    listUsersByPage();
                    break;
                case 7:
                    getUserByNo();
                    break;
                case 0:
                    System.out.println("感谢使用，再见！");
                    System.exit(0);
                default:
                    System.out.println("无效的选择，请重新输入！");
            }
        }
    }

    private static void addUser() {
        System.out.println("\n--- 添加新用户 ---");

        System.out.print("请输入用户编号: ");
        String userno = scanner.nextLine();

        System.out.print("请输入用户姓名: ");
        String userName = scanner.nextLine();

        System.out.print("请输入密码: ");
        String password = scanner.nextLine();

        System.out.print("请输入职位: ");
        String job = scanner.nextLine();

        System.out.print("请输入薪水: ");
        double sal = scanner.nextDouble();
        scanner.nextLine(); // 消耗换行符

        User user = new User(userno, userName, password, job, sal);
        userDao.insert(user);
        System.out.println("用户添加成功！");
    }

    private static void userLogin() {
        System.out.println("\n--- 用户登录 ---");

        System.out.print("请输入用户编号: ");
        String userno = scanner.nextLine();

        System.out.print("请输入密码: ");
        String password = scanner.nextLine();

        boolean success = userDao.login(userno, password);
        if (success) {
            System.out.println("登录成功！");
        } else {
            System.out.println("登录失败，用户编号或密码错误！");
        }
    }

    private static void updateUser() {
        System.out.println("\n--- 修改用户信息 ---");

        System.out.print("请输入要修改的用户编号: ");
        String userno = scanner.nextLine();

        User user = userDao.getUserByNo(userno);
        if (user == null) {
            System.out.println("用户不存在！");
            return;
        }

        System.out.println("当前用户信息: " + user);

        System.out.print("请输入新用户名(直接回车不修改): ");
        String userName = scanner.nextLine();
        if (!userName.isEmpty()) {
            user.setUserName(userName);
        }

        System.out.print("请输入新密码(直接回车不修改): ");
        String password = scanner.nextLine();
        if (!password.isEmpty()) {
            user.setPassword(password);
        }

        System.out.print("请输入新职位(直接回车不修改): ");
        String job = scanner.nextLine();
        if (!job.isEmpty()) {
            user.setJob(job);
        }

        System.out.print("请输入新薪水(输入0表示不修改): ");
        double sal = scanner.nextDouble();
        scanner.nextLine(); // 消耗换行符
        if (sal != 0) {
            user.setSal(sal);
        }

        userDao.update(user);
        System.out.println("用户信息更新成功！");
    }

    private static void deleteUsers() {
        System.out.println("\n--- 删除用户 ---");

        System.out.print("请输入要删除的用户编号(多个编号用逗号分隔): ");
        String input = scanner.nextLine();
        String[] usernos = input.split(",");

        // 去除空格
        for (int i = 0; i < usernos.length; i++) {
            usernos[i] = usernos[i].trim();
        }

        System.out.print("确定要删除这些用户吗？(y/n): ");
        String confirm = scanner.nextLine();
        if (confirm.equalsIgnoreCase("y")) {
            userDao.delete(usernos);
            System.out.println("用户删除成功！");
        } else {
            System.out.println("已取消删除操作");
        }
    }

    private static void listAllUsers() {
        System.out.println("\n--- 所有用户列表 ---");

        List<User> users = userDao.getAll();
        if (users.isEmpty()) {
            System.out.println("--- 没有用户记录 ---");
        } else {
            for (User user : users) {
                System.out.println(user);
            }
        }
        System.out.println("总计: " + users.size() + " 个用户");
    }

    private static void listUsersByPage() {
        System.out.println("\n--- 分页查询用户 ---");

        System.out.print("请输入每页显示数量: ");
        int pageSize = scanner.nextInt();

        System.out.print("请输入要查看的页码: ");
        int pageNum = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符

        List<User> users = userDao.getUserForPage(pageSize, pageNum);

        System.out.println("\n第 " + pageNum + " 页用户列表:");

        if (users.isEmpty()) {
            System.out.println("--- 没有用户记录 ---");
        } else {
            for (User user : users) {
                System.out.println(user);
            }
        }
        System.out.println("当前页显示: " + users.size() + " 个用户");
    }

    private static void getUserByNo() {
        System.out.println("\n--- 按编号查询用户 ---");

        System.out.print("请输入用户编号: ");
        String userno = scanner.nextLine();

        User user = userDao.getUserByNo(userno);
        if (user != null) {
            System.out.println("\n用户信息:");
            System.out.println(user);
        } else {
            System.out.println("没有找到编号为 " + userno + " 的用户");
        }
    }
}